//
//  ICContactTreeCell.m
//  ICome
//
//  Created by zhangrongwu on 16/3/22.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICContactTreeCell.h"
#import "UIImage+Extension.h"
#import "UIImage+AvatarGeneration.h"

#define headImageHeight (33.0 * Level)
@implementation ICContactTreeCell
- (id)initWithStyle:(UITableViewCellStyle)cell reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:cell reuseIdentifier:reuseIdentifier];
    if (self) {
        self.bottomLine.hidden = YES;
        [self layoutSubview];
    }
    return self;
}

- (void)layoutSubview {
    [self.selectButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImageView.mas_right).offset(6);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    [self.subTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(6);
        make.centerY.equalTo(self.mas_centerY);
    }];
}

-(void)setModel:(ICEmpInfoModel *)model {
    _model = model;
    // 初始化状态  - 缺陷 disable 与 highlighted 均不能点击选中
    self.selectButton.hidden = YES;
    self.selectButton.enabled = YES;
    
    // Button 是否选中
    if (model.disable) {
        self.selectButton.userInteractionEnabled = NO;
        self.selectButton.highlighted = model.disable;
    } else {
        self.selectButton.userInteractionEnabled = YES;
        self.selectButton.highlighted = model.disable;
    }

    // Button 是否需要显示
    if ((self.model.CtrStyle == CtrStyleEmploye_sdk || self.model.CtrStyle == CtrStyleSelectEmploye_sdk || self.model.CtrStyle == CtrStyleSelectNode_sdk)) {
        self.selectButton.hidden = NO;
        
        if (model.isExistingUser) {// 历史人员
            self.selectButton.selected = NO;
            self.selectButton.enabled = NO;
        } else {
            self.selectButton.selected = model.select;
            self.selectButton.enabled = YES;
        }
        
        
        [self.selectButton addTarget:self action:@selector(selectClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.selectButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(10 * model.rowLevel + 15);
            make.centerY.equalTo(self.mas_centerY);
            make.width.mas_equalTo(40 * Level);
            make.height.mas_equalTo(40 * Level);
        }];
    } else {
        self.selectButton.hidden = YES;
        [self.selectButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(10 * model.rowLevel);
            make.centerY.equalTo(self.mas_centerY);
        }];
    }
    
    NSString *title = @"";
    NSString *subTitle = @"";
    if (![ICTools stringEmpty:model.oName]) {
        if ((self.model.CtrStyle == CtrStyleEmploye_sdk || self.model.CtrStyle == CtrStyleSelectEmploye_sdk || self.model.CtrStyle == CtrStyleSelectNode_sdk)) {
            if (model.count == 0) {
                title = model.oName;
            } else {
                title = [NSString stringWithFormat:@"%@(%ld)", model.oName, (long)model.count];
            }
        } else {
            title = model.oName;
        }
        if (model.isOpen) {
            self.headImageView.image = [UIImage imageNamedFromiComeSDKBundle:@"icon_contact_open"];
        } else {
            self.headImageView.image = [UIImage imageNamedFromiComeSDKBundle:@"icon_contact_close"];
        }
        
        if (model.isLastRootNode) {
            self.contentView.backgroundColor = TABLECELLSELECTCOLOR;
        } else {
            self.contentView.backgroundColor = [UIColor whiteColor];
        }
        [self fitnessHeadImageViewisEmpemp:NO];
    } else if (![ICTools stringEmpty:model.eName]) {
        title = model.eName;
        subTitle = model.jobTitle;
        
        NSString *URL = [ICTools getMinImageURL:(model.photoId)];
        [UIImage addAvatariamgeView:self.headImageView withUrlStr:URL witheId:model.eId withName:model.eName];

        
        [self fitnessHeadImageViewisEmpemp:YES];
        if ([model.eId isEqualToString:[ICUser currentUser].eId]) {
            self.contentView.backgroundColor = TABLECELLSELECTCOLOR;
        } else {
            self.contentView.backgroundColor = [UIColor whiteColor];
        }
    }
    
    self.titleLabel.text = title;
    self.subTitleLabel.text = subTitle;
    
}


- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_headImageView];
    }
    return _headImageView;
}

-(UIImageView *)statusImageView {
    if (!_statusImageView) {
        _statusImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_statusImageView];
        
    }
    return _statusImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:17];
        _titleLabel.preferredMaxLayoutWidth = 200 / 375 * App_Frame_Width;
        [_titleLabel sizeToFit];
        _titleLabel.textColor = ICTEXTCOLOR_T1;
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}

-(UILabel *)subTitleLabel {
    if (!_subTitleLabel) {
        _subTitleLabel = [[UILabel alloc] init];
        _subTitleLabel.font = [UIFont systemFontOfSize:12];
        _subTitleLabel.preferredMaxLayoutWidth = 200;
        [_subTitleLabel sizeToFit];
        _subTitleLabel.textColor = ICTEXTCOLOR_T2;
        [self.contentView addSubview:_subTitleLabel];
    }
    return _subTitleLabel;
}

- (UIButton *)selectButton {
    if (!_selectButton) {
        _selectButton = [[UIButton alloc] init];
        [_selectButton setImage:[UIImage imageNamedFromiComeSDKBundle:@"App_select_dis"] forState:UIControlStateNormal];
        [_selectButton setImage:[UIImage imageNamedFromiComeSDKBundle:@"App_select"] forState:UIControlStateSelected];
        [_selectButton setImage:[UIImage imageNamedFromiComeSDKBundle:@"App_select_disable"] forState:UIControlStateHighlighted];
        [_selectButton setImage:[UIImage imageNamedFromiComeSDKBundle:@"App_select_existing"] forState:UIControlStateDisabled];
        
        _selectButton.hidden = YES;
        [self.contentView addSubview:_selectButton];
    }
    return _selectButton;
}
// 处理headimageview
- (void)fitnessHeadImageViewisEmpemp:(BOOL)emp {
    if (emp) {
        [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.selectButton.mas_right).offset(10);
            make.centerY.equalTo(self.mas_centerY);
            make.width.height.mas_equalTo(headImageHeight);
        }];
        self.headImageView.layer.masksToBounds = YES;
        self.headImageView.layer.cornerRadius = 5;
        
        //  1. 页面类型  2. 会话类型
        if ((self.model.CtrStyle == CtrStyleEmploye_sdk || self.model.CtrStyle == CtrStyleSelectEmploye_sdk || self.model.CtrStyle == CtrStyleSelectNode_sdk) && (self.model.kkGroupType == GroupStyleCreateNode || self.model.kkGroupType == GroupStyleSelectNode)) {
            [_selectButton setImage:[UIImage new] forState:UIControlStateHighlighted];
        } else {
            [_selectButton setImage:[UIImage imageNamedFromiComeSDKBundle:@"App_select_disable"] forState:UIControlStateHighlighted];
        }
    } else {
        self.headImageView.layer.masksToBounds = NO;
        self.headImageView.layer.cornerRadius = 0/2;
        //  1. 页面类型  2. 会话类型
        if (((self.model.CtrStyle == CtrStyleEmploye_sdk || self.model.CtrStyle == CtrStyleSelectEmploye_sdk || self.model.CtrStyle == CtrStyleSelectNode_sdk) && self.model.kkGroupType == GroupStyleCreateEmployee) || self.model.kkGroupType == GroupStyleSelectEmployee) {
            [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.selectButton.mas_right).offset(-20);
                make.centerY.equalTo(self.mas_centerY);
                make.width.height.mas_equalTo(15 * Level);
            }];
            [_selectButton setImage:[UIImage new] forState:UIControlStateHighlighted];
        } else {
            [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.selectButton.mas_right).offset(8);
                make.centerY.equalTo(self.mas_centerY);
                make.width.height.mas_equalTo(15 * Level);
            }];
            // 对选择人员关闭禁止按钮
            [_selectButton setImage:[UIImage imageNamedFromiComeSDKBundle:@"App_select_disable"] forState:UIControlStateHighlighted];
        }
    }
}

- (void)selectClicked:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(didClickSelectButtonInCell:)]) {
        [self.delegate didClickSelectButtonInCell:self];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

@end

